﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model;
using Gift.DAL;
using AjaxPro;

namespace WebSite.cn
{
    public partial class account : BasePage
    {
        readonly DALRegion theRegion = new DALRegion();
        tbMember oMember;
        readonly DALCar theCar = new DALCar();
        // Fields
        public string address = "";
    
        public string cname = "";
        public string email = "";

        public string phone = "";
        public string post = "";
        public string tel = "";
        public string accountmoney = "0";
        public string sendDate = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");

        public string isHaveCard = "0";
        public string isHaveProfession = "0";
        public string isHaveShop = "0";
        public string tempOrderCode = "";

        public string UserCardTotalPrice = "";
        public string VisibleLogin = "none";
        readonly MemberAccount theMemberAccount = new MemberAccount();
        readonly Member theMember = new Member();
        readonly OrderAccount theOrderAccount = new OrderAccount();
        readonly BasePage thePage = new BasePage();

        string _deliveryTemplateId = "0";

        public string DeliveryTemplateId
        {
            get { return _deliveryTemplateId; }
        }

        public string TempType
        {
            get
            {

                if (base.LoginUserMember != null && base.LoginUserMember.DicUserCardList != null)
                {
                    return "1";
                }
                else if (base.LoginUserMember != null)
                {
                    return "2";
                }
                else
                {
                    return Request.QueryString["type"];
                }
            }
        }

        public string SendDate
        {
            get
            {
                return DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");
            }
        }

        public string BackHref
        {
            get
            {
                if (Session["backHref"] != null)
                {
                    return Session["backHref"].ToString() + "&isView=1";
                }
                else
                {
                    return "#";
                }
            }
        }

        public string ServerSendDate
        {
            get
            {
                return DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");
            }
        }
        public string ToDate
        {
            get
            {
                return DateTime.Now.AddDays(5).ToString("yyyy-MM-dd");
            }
        }

        // Methods
        private void Bind()
        {
          
            if (base.LoginUserMember != null && base.LoginUserMember.IsCard == false)
            {
                VisibleLogin = "none";
                oMember = theMember.GetModel(int.Parse(base.LoginUserMember.MemberId));

                if (oMember != null)
                {
                    this.cname = oMember.membername;
                    this.tel = oMember.tel1;
                    this.phone = oMember.phone;
                    this.post = oMember.post;
                    this.address = oMember.address;
                    this.email = oMember.email;
                    //  this.sendDate = table.Rows[0]["sendDate"].ToString();
                }
            }
            else
            {
                VisibleLogin = "block";
            }
        }

   

        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(WebSite.Code.CarGoodsInfo_ajax));
           
            if (!base.IsPostBack)
            {
            
                this.BindProvenceList();
                try
                {
                    this.Bind();
                    this.GetAccountMoney();
                    if (base.LoginUserMember != null && base.LoginUserMember.DicUserCardList != null)
                    {

                        decimal dTotalCardPrice = 0;
                        foreach ( int key in base.LoginUserMember.DicUserCardList.Keys)
                        {
                            dTotalCardPrice += base.LoginUserMember.DicUserCardList[key].cardprice;
                        }
                        UserCardTotalPrice = dTotalCardPrice.ToString();

                    }
                    this.tempOrderCode = theOrderAccount.GenerateOrderCodeForBefor();
                }
                catch (Exception ex)
                {
                    LogFile.Write(ELogType.Error, ex.Message);
                    //  LogFile.SendEmail(ex.Message);
                }

            }
        }

        public string GetAccountMoney()
        {

            if (base.LoginUserMember == null || string.IsNullOrEmpty(base.LoginUserMember.MemberId))
            {
                return "";
            }
            else
            {
                return string.Format("您账户内的福点数为：<font color=\"red\"><b>{0}</b> </font>福点"
                                      , theMemberAccount.GetRemainSumMoney(int.Parse(base.LoginUserMember.MemberId)).ToString()
                                      );

            }
        }

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            base.LoginUserMember.DicUserCardList = null;
            Response.Redirect("index.aspx");
        }



        void BindProvenceList()
        {
            DataTable table = null ;

//=======================================通用商品============================
            if (thePage.LoginUserMember != null)
            {
                if (thePage.LoginUserMember.IsCard)
                {
                    table = theCar.GetListByMemberId(long.Parse(thePage.LoginUserMember.TempMembertId), 1).Tables[0];
                }
            }
            else
            {
                if (re.ReadCookie("tempnoCard") != null)
                {
                    try
                    {
                        table = theCar.GetListByMemberId(long.Parse(re.ReadCookie("tempnoCard")), 1).Tables[0];
                    }
                    catch
                    {
                    }
                }
            }
            if (table != null && table.Rows.Count > 0)
            {
                _deliveryTemplateId = table.Rows[0]["deliveryTemplateId"].ToString();
                DataTable table2 = theRegion.GetListByDeliveryTemplateId(0,int.Parse(table.Rows[0]["deliveryTemplateId"].ToString()), 1).Tables[0];
                this.ddlProvence.DataTextField = "regionName";
                this.ddlProvence.DataValueField = "Id";
                this.ddlProvence.DataSource = table2;
                this.ddlProvence.DataBind();
                this.ddlProvence.Items.Insert(0, new ListItem("请选择省", ""));

            }
            else
            {

                //=======================================专用卡商品==============================================
                DataTable tbCardGoods = null;
                if (thePage.LoginUserMember != null)
                {
                    tbCardGoods = theCar.GetListByMemberId(long.Parse(thePage.LoginUserMember.MemberId), 0).Tables[0];
                }
                else
                {

                    if (re.ReadCookie("tempnoGenerate") != null)
                    {
                        try
                        {
                            tbCardGoods = theCar.GetListByMemberId(long.Parse(re.ReadCookie("tempnoGenerate")), 0).Tables[0];
                        }
                        catch
                        {
                        }
                    }
                }

                if (tbCardGoods != null && tbCardGoods.Rows.Count > 0)
                {
                    _deliveryTemplateId = tbCardGoods.Rows[0]["deliveryTemplateId"].ToString();
                    DataTable table2 = theRegion.GetListByDeliveryTemplateId(0,int.Parse(tbCardGoods.Rows[0]["deliveryTemplateId"].ToString()), 1).Tables[0];
                    this.ddlProvence.DataTextField = "regionName";
                    this.ddlProvence.DataValueField = "Id";
                    this.ddlProvence.DataSource = table2;
                    this.ddlProvence.DataBind();
                    this.ddlProvence.Items.Insert(0, new ListItem("请选择省", ""));
                }
            }

        }

  




    }
}